const express = require("express")
const { v4 } = require("uuid")
const { query } = require("../db/db")
// const getIpAddress = require('../middleware/getIpAddress')
const router = express.Router()

router.get("/list", (req, res) => {
  const sql = `select * from blog_message order by create_time desc`
  const value = []
  query(sql, value)
    .then((data) => {
      res.json({ ...data, locationInfo: res.locationInfo })
    })
    .catch((err) => {
      res.json(err)
    })
})

router.post("/list", (req, res) => {
  console.log(req.body)
  const { message, author = "袁宏星" } = req.body
  const sql = `insert into blog_message (id, message,author,create_time) values (?,?,?,?)`
  const value = [
    v4().toString().replace(/-/g, ""),
    message,
    author,
    new Date()
  ]
  query(sql, value)
    .then((data) => {
      res.json(data)
    })
    .catch((err) => {
      res.json(err)
    })
})

module.exports = router
